import 'package:development_assistant/controller/test_controller.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../config/colors.dart';
import '../../../../config/font.dart';
import '../../../../model/ChartData.dart';
import '../../widget/custom_text.dart';


class TestDetail extends StatelessWidget {
  final TestController testController=Get.put(TestController());
  final testIndex=Get.arguments[0];
  TestDetail({super.key});


  @override
  Widget build(BuildContext context) {


    return Scaffold(
      appBar: _appBar(),
      resizeToAvoidBottomInset: false,
      bottomNavigationBar: _deleteTest(),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(top: 20.h,left: 20.w,right: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _testName(),
              SizedBox(height: 10.w,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _testEnv("测试环境".tr, testController.testList[testIndex]['test_env'].toString().tr),
                  _testField("测试线程".tr, testController.testList[testIndex]['test_thread'])
                ],
              ),
              SizedBox(height: 10.w,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _testField("间隔时长".tr,testController.testList[testIndex]['test_interval']),
                  _testField("循环次数".tr, testController.testList[testIndex]['test_cycles']),
                ],
              ),
              SizedBox(height: 10.w,),
              _testStep(),
              _testCharts()
            ],
          ),
        ),
      ),
    );
  }

  _deleteTest(){
    return Container(
      width: 200.w,
      height: 35.h,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 100.w),
      child: ElevatedButton(
        onPressed: (){
          Get.defaultDialog(
            title: "删除测试数据".tr,
            middleText: "${'是否删除该测试数据?'.tr}: ${testController.testList[testIndex]["test_name"]}",
            confirm: TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                testController.delTest(testController.testList[testIndex]["test_id"]);
              },
              child: Text("确定".tr),
            ),
            cancel:TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                Get.back();
              },
              child: Text("取消".tr),
            ),
          );
        },
        style: ElevatedButton.styleFrom(
            backgroundColor: ColorsConfig().buttonColorOfTheme,
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20)
            )
        ),
        child: Text("删除测试数据".tr,style: TextStyle(fontWeight: FontWeight.bold),),
      ),
    );
  }

  _testName(){
    return SizedBox(
      width: 320.w,
      height: 40.h,
      child: Center(
        child: TextField(
          controller: TextEditingController(text: testController.testList[testIndex]['test_name'] ),
          maxLines: 1,
          decoration: InputDecoration(
              hintText:"自动化测试名称".tr,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){
          },
        ),
      ),
    );
  }

  _testEnv(String hintText,String testValue){
    return SizedBox(
      width: 150.w,
      height: 40.h,
      child: Center(
        child: TextField(
          maxLines: 1,
          controller: TextEditingController(text:testValue),
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){

          },
        ),
      ),
    );
  }

  _testField(String hintText,int testValue){
    return SizedBox(
      width: 150.w,
      height: 40.h,
      child: Center(
        child: TextField(
          maxLines: 1,
          controller: TextEditingController(text:testValue.toString()),
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){

          },
        ),
      ),
    );
  }

  _testStep(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("测试步骤".tr,style: FontConfig.fieldTitle,),
        //参数列表
        Obx(() => Wrap(
          children: List<Widget>.generate(testController.testApiList[testIndex].length, (index) {
            return Container(
              height: 70.h,
              margin: EdgeInsets.only(bottom: 10.h),
              padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 15.h),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(color:Colors.grey,width: 1)
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Container(
                    width: 45.w,
                    height: 45.w,
                    margin: EdgeInsets.symmetric(horizontal: 10.w),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(50),
                        color: ColorsConfig.methodTagColors[testController.testApiList[testIndex][index]["api_method"]]
                    ),
                    child: Center(child: Text(testController.testApiList[testIndex][index]["api_method"],style: TextStyle(color: Colors.white),)),
                  ),
                  SizedBox(
                    width: 190.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        CustomText(text: testController.testApiList[testIndex][index]["api_name"], textStyle: FontConfig.apiName),
                        const SizedBox(height: 5,),
                        CustomText(text: testController.testApiList[testIndex][index]["api_url"], textStyle: FontConfig.urlStyle)
                      ],
                    ),
                  ),
                ],
              ),
            );
          }),
        ))
      ],
    );
  }

  _charts(){
    var seriesList = [
      charts.Series(
        id: 'Pie',
        domainFn: (ChartData data, _) => data.pieLabel,
        measureFn: (ChartData data, _) => data.pieValue,
        data: [
          ChartData('success', testController.testList[testIndex]["test_success"]),
          ChartData('error', testController.testList[testIndex]["test_error"]),
        ],
      ),
    ];
    return SizedBox(
        width: 130.w,
        height: 130.w,
        child: charts.PieChart(
          seriesList,
          animate: true,
        )
    );
  }

  _testData(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Container(
              width: 10.w,
              height: 10.w,
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Colors.green
              ),
            ),
            Text("${'成功次数'.tr} : ${testController.testList[testController.pickIndex.value]["test_success"]} ",style: FontConfig.fieldTitle,),
          ],
        ),
        Row(
          children: [
            Container(
              width: 10.w,
              height: 10.w,
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Colors.green
              ),
            ),
            Text("${'失败次数'.tr} : ${testController.testList[testController.pickIndex.value]["test_error"]} ",style: FontConfig.fieldTitle,),
          ],
        )
      ],
    );
  }

  _testCharts(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("测试数据".tr,style: FontConfig.fieldTitle,),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 20.w,vertical: 10.h),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              border: Border.all(color:Colors.grey,width: 1)
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  _charts(),
                  _testData(),
                ],
              ),
              CustomText(text: "${'循环次数'.tr} : ${testController.testList[testIndex]["test_cycles"]} ", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 15.sp)),
              SizedBox(height: 3.h,),
              CustomText(text: "${'测试总耗时'.tr} : ${testController.testList[testIndex]["request_time"]} ms", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 15.sp)),
              SizedBox(height: 3.h,),
              CustomText(text: "${'平均接口请求耗时'.tr} : ${testController.testList[testIndex]["average_time"]} ms", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 15.sp)),
              SizedBox(height: 3.h,),
              CustomText(text: "${'测试时间'.tr} : ${testController.testList[testIndex]["test_date"]}", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 15.sp)),
            ],
          ),
        ),
      ],
    );
  }

  _appBar(){
    return AppBar(
      title: Text("测试详情".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          Get.offNamed("/Home",arguments: {"selectIndex":2});
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }
}
